package server;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * 把请求信息封装为Request对象（根据InputSteam输入流封装）
 */
public class Request {

    private String method; // 请求方式，比如GET/POST
    private String url;  // 例如 /,/index.html
    private String host; //请求的主机名称
    private InputStream inputStream;  // 输入流，其他属性从输入流中解析出来


    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Request() {
    }


    // 构造器，输入流传入
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        int count = 0;
        while (count == 0) {
            count = this.inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String instr = new String(bytes);
        String[] split = instr.split("\n");
        String firstLine = split[0];
        String[] s = firstLine.split(" ");
        String secLine = split[1];
        String[] s1 = secLine.split(" ");
        this.method = s[0];
        this.url = s[1];
        String[] split1 = s1[1].split(":");
        this.host = split1[0];
        System.out.println("=====>>host:" + host);
        System.out.println("=====>>method:" + method);
        System.out.println("=====>>url:" + url);
    }
}
